/**
 * 核心技术支持
 * IT渠道网 小程序商城
 * 13873174181 微信同号
 */
import {Address} from '../../utils/address.js';
var address = new Address();

Page({
    data: {
        name: "",
        mobile: "",
        detail: "", //详址
        area: null,
        areaDetail: null, //{province,city,district}
        sname: "保存",
        state: 0,
        selected: null, //更新实例
        isAreaPicker: false,
        provinceData: [],
        cityData: [],
        districtData: [],
        pIndex: 0,
        cIndex: 0,
        dIndex: 0
    },
    /**
     * 初始化area-picker
     */
    onLoad: function(option) {
        var that = this;
        if (option.index != null) {
            var addressList = address.getAddressListFromLocal(),
                selected = addressList[option.index];
            // 转换省市区
            var area = {
                    province: selected.receiverState,
                    city: selected.receiverCity,
                    district: selected.receiverDistrict
                };
            var areaDetail = this.checkArea(area);
            this.setData({
                name: selected.receiverName,
                mobile: selected.receiverMobile,
                detail: selected.receiverAddress,
                selected: selected,
                areaDetail: areaDetail,
                sname: "更新",
                state: 1
            })
        }
        // 初始化省市区数据 预加载
        address.getProvinceData((res)=> {
            that.setData({
               provinceData: res.data
            })
        })
        that.getCityData(1);
    },
    hideAreaPicker: function() {
        this.setData({
            isAreaPicker: false,
            pIndex: 0,
            cIndex: 0,
            dIndex: 0
        })
    },
    showAreaPicker: function() {
        var that = this;
        that.setData({
            isAreaPicker: true
        })
    },
    getCityData: function(id) {
        var that = this;
        address.getCityData(id,(res)=> {
            that.setData({
               cityData: res.data,
               //cIndex: 0,
               //dIndex: 0
            })
            // 获取区数据
            var cid = res.data[that.data.cIndex].id; //已选cid
            address.getDistrictData(cid,(res)=> {
                that.setData({
                   districtData: res.data
                })
            })
        })
    },
    getDistrictData: function(id) {
        var that = this;
        address.getDistrictData(id,(res)=> {
            that.setData({
               districtData: res.data
            })
        })
    },
    /**
     * 响应 所在地区
     */
    areaPickerChange: function(event) {
        var that = this,
            arr = event.detail.value,
            pIndex = arr[0],
            cIndex = arr[1],
            dIndex = arr[2];
        // 判断索引是否发生变化
        if (pIndex != that.data.pIndex) {
            // 重新获取市,区
            var pid = that.data.provinceData[pIndex].id;
            that.getCityData(pid);
        }
        if (pIndex == that.data.pIndex && cIndex != that.data.cIndex) {
            // 重新获取区
            var cid = that.data.cityData[cIndex].id;
            that.getDistrictData(cid);
        }
        that.setData({
           pIndex: pIndex,
           cIndex: cIndex,
           dIndex: dIndex
        })
    },
    /**
     * 确定地区
     */
    areaPickerConfirm: function() {
        var that = this,
            area = {
                province: that.data.provinceData[that.data.pIndex].name,
                city: that.data.cityData[that.data.cIndex].name,
                district: that.data.districtData[that.data.dIndex].name
            };
        var areaDetail = this.checkArea(area);
        that.setData({
            area: area,
            areaDetail: areaDetail,
            isAreaPicker: false
        })
    },
    /**
     * 地址处理
     */
    checkArea: function(area) {
        var areaDetail = area.province +' '+ area.city +' '+ area.district;
        if (address.isCenterCity(area.province)) {
            areaDetail = area.province +' '+ area.district;
        }
        return areaDetail;
    },
    /**
     * 绑定input
     */
    inputBlur: function(e) {
        var t = '{"' + e.currentTarget.dataset.name + '":"' + e.detail.value + '"}';
        this.setData(JSON.parse(t));
    },
    /**
     * 保存/更新地址
     */
    saveAddress: function() {
        var that = this;
        // 更新 封装数据
        if (that.data.state == 1) {
            var data = this._setAddress();
            address.updateAddress(data, (flag)=> {
                if(!flag){
                    that.showTips('操作提示', '地址信息更新失败！')
                }
            })
        } else {
            // 增加 封装数据
            var data = this._setAddress();
            address.submitAddress(data, 1, (flag)=> {
                if(!flag){
                    that.showTips('操作提示', '地址信息更新失败！')
                }
            })
        }
        // 返回上级
        wx.navigateBack({
            delta: 1
        })
    },
    /**
     * 重新组装请求保存地址数据{}
     */
    _setAddress: function() {
        var addr = this.data.selected,
            area = this.data.area;
        if (addr == null) {
            addr = {}
            addr.df = 0;
        }
        addr.receiverName = this.data.name;
        addr.receiverMobile = this.data.mobile;
        addr.receiverAddress = this.data.detail;
        if (area != null) {
            addr.receiverState = area.province;
            addr.receiverCity = area.city;
            addr.receiverDistrict = area.district;
        }
        return addr;
    },
    /**
     * 一键授权
     */
    getWechatAddress: function(e) {
        
    }
})